<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('user_mdl');
    }

    public function index(){
        //分页
        $url = '';
        //$this->db->start_cache();

        $this->load->library('pagination');
        $this->config->load('page', TRUE);
        $config = $this->config->config;
        $config = $config['page'];
        $config['base_url'] = site_url('user/index').$url;
        $config['total_rows'] = $this->user_mdl->get_count();

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('update_time desc');
        $list = $this->user_mdl->select();
        $data['rows'] = $config['total_rows'];
        //echo $this->db->last_query();
        //$this->db->flush_cache();
        $_group = $this->user_mdl->_group;

        foreach($list as $k => $v){
            $list[$k]['group_name'] = $_group[$v['group']];
        }

        $data['list'] = $list;
        $this->_view('index',$data);
    }

    /**
     * 用户新增
     */
    public function create(){
        if($_SESSION['user']['group'] != 1){
            show_404();
        }

        if($this->input->is_ajax_request()){
            $this->user_mdl->_validation();
            $this->form_validation->set_rules('email', '邮箱', 'required|valid_email|is_unique[user.email]',[
                'required' => '请填写%s地址',
                'valid_email' => '请填写合法的%s',
                'is_unique' => '该%s已经被注册，不能注册'
            ]);
            $this->form_validation->set_rules('phone', '联系电话', 'required|regex_match[/^[0-9-]{7,16}/]',[
                'required' => '请填写%s',
                'regex_match' => '请输入正确的%s'
                ]);
            if($this->form_validation->run() == FALSE){
                echo validation_errors();
            }else{
                $post = $this->input->post();
                $res = $this->user_mdl->add($post);
                echo $res === false ? '添加失败！':'1';
            }
        }else{
            $this->_view('create');
        }

    }

    /**
     * 密码重置
     */
    public function passconf(){
        $uid = $_SESSION['user']['id'];
        if($_SESSION['user']['group'] == 1){
            $uid = $this->input->get('uid');
        }

        if($this->input->is_ajax_request()){
            $this->user_mdl->_validation();
            $this->form_validation->set_rules('phone', '联系电话', '');
            if($this->form_validation->run() == FALSE){
                echo validation_errors();
            }else{
                $post = $this->input->post();
                if(empty($post['uid'])){
                    die('参数错误！');
                }
                $post['password'] = md5($post['password']);
                unset($post['password_v']);
                $res = $this->user_mdl->edit($post);
                echo $res === false ? '添加失败！':'1';
            }
        }else{
            $this->db->where('id',intval($uid));
            $this->db->select('id,name,email');
            $res = $this->user_mdl->get_one();

            if(empty($res)){
                show_error('参数错误！');
            }
            $this->_view('passconf',['res'=>$res]);
        }
    }

    /**
     * 用户修改
     */
    public function update(){
        if($_SESSION['user']['group'] != 1){
            show_404();
        }

        if($this->input->is_ajax_request()){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('phone', '联系电话', 'required|regex_match[/^[0-9-]{7,16}$/]',[
                'required' => '请填写%s',
                'regex_match' => '请输入正确的%s'
                ]);
            $this->form_validation->set_rules('name', '用户名', 'required|regex_match[/^.{1,30}$/]',[
                'required' => '请填写%s',
                'regex_match' => '请输入正确的%s,用户名为任意字符串，1-15个字符'
                ]);
            $this->form_validation->set_error_delimiters('<span class="am-icon-close am-icon-sm">  ', '</span><br/>');

            if($this->form_validation->run() == FALSE){
                echo validation_errors();
            }else{
                $post = $this->input->post();
                if(empty($post['uid'])){
                    die('参数错误！');
                }
                $res = $this->user_mdl->edit($post);
                echo $res === false ? '添加失败！':'1';
            }
        }else{
            $uid = $this->input->get('uid');
            $this->db->where('id',intval($uid));
            $res = $this->user_mdl->get_one();
            if(empty($res)){
                show_error('参数错误！');
            }
            $this->_view('update',['res'=>$res]);
        }
    }

    /**
     * 用户详情
     */
    public function show(){
        $uid = $this->input->get('uid');
        $this->db->where('id',intval($uid));
        $res = $this->user_mdl->get_one();
        $this->_view('show',['res'=>$res]);
    }
    /**
     * 删除
     */
    public function delete(){
        if($_SESSION['user']['group'] == 3){
            show_404();
        }

        $get = $this->input->get('cid');
        $this->db->where('id',$get);
        $res = $this->user_mdl->delete();
        echo $res === false ? '删除失败！': redirect(site_url('user/index'));
    }

    /**
     * 全选删除
     */
    public function del(){
        if($_SESSION['user']['group'] == 3){
            show_404();
        }

        $get = $this->input->get('id');
        $get = explode(",",$get);
        //print_r($get);exit;
        for($i=0;$i<count($get);$i++){
            $this->db->where('id',$get[$i]);
            $res = $this->user_mdl->delete();
        }
        
        echo $res === false ? '删除失败！': 1;
    }

    /**
     * 用户退出
     */
    public function user_out(){
        $this->session->sess_destroy();
        redirect(site_url('home/index'));
    }

}

/* End of file User.php */
/* Location: ./application/controllers/User.php */